package com.lerays.yuan.AI_Robot;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lionsoul.jcseg.core.JcsegException;

import com.lerays.yuan.AI_Robot.jcseg.JcsegTest;
import com.lerays.yuan.AI_Robot.util.CommonParam;
import com.lerays.yuan.AI_Robot.util.Connect;
import com.lerays.yuan.AI_Robot.util.DB_Connect;
 
/**
 * @author Himi
 */
public class AIServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String msg = "";  
    private DB_Connect conn;
    private ResultSet rs;
    private String  sentence;
    private String keyword;
    private ArrayList<String> reply = new ArrayList<String>();
    private String type = "";
    private CommonParam cp;
    private String firstSentence;
    private String secondSentence;
 
    public AIServlet() {
           conn = new Connect().connectDb();
           cp = new CommonParam();
    }  
 
    public AIServlet(String msg) {
        this.msg = msg;
    }
    
    //根据类型回答
    public String typePrint(String str){
            String g = cp.getString(str);
            String arr[] = g.split(",");
            int random = (int)(Math.random()*arr.length);     //选取随机回答
            return arr[random];
            //response.getWriter().println(arr[random]);
    }
    
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        sentence = request.getParameter("sentence");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
       
        if(sentence==null)
        {
                response.getWriter().println("没内容");
        } else{
                // 判断是否重复
                if (sentence.equals(firstSentence)) {
                        if (sentence.equals(secondSentence)) {
                                response.getWriter().println(typePrint("repeat"));
                                return;
                        } else{
                                secondSentence = sentence; 
                        }                      
                } else {
                        firstSentence = sentence;
                }
           
                //开始分词
                JcsegTest demo;
                try {
                       demo = new JcsegTest();
                       keyword = demo.segment(sentence);
                       type = demo.getType();
                       //System.out.println("keyword="+keyword);                        
                } catch (JcsegException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
               //分词结束
                
                //分析词性
               if (type.equals("greeting")) {             
                       response.getWriter().println(typePrint("greeting"));
               } else if(type.equals("bye")) {                    
                       response.getWriter().println(typePrint("bye"));
               } else{
                       ArrayList field = new ArrayList();
                       field.add("content");
                       rs = conn.dbSelect("reply", field," where key_id=(select Id from key_words where word='"+keyword+"')");
                       try {
                               while (rs.next())
                               {
                                 reply.add(rs.getString("content"));
                                 // System.out.println(content);
                                 //response.getWriter().println(content);
                               }
                       } catch (SQLException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                       }
                       //关键字没收录
                       if(reply.isEmpty()){
                               response.getWriter().println(typePrint("none"));
                       } else {
                               int random = (int)(Math.random()*reply.size());     //选取随机回答
                               response.getWriter().println(reply.get(random));
                               reply.clear();
                       } 
               }
        }
    }
}